
package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWage;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.service.BtyWageService;
import cn.stylefeng.guns.sys.modular.system.service.BtyWxService;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.guns.sys.modular.system.warpper.WageWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 *
 * @author fengshuonan
 * @Date 2017-05-09 23:02:21
 */
@Controller
@RequestMapping("/btyWage")
public class BtyWageController extends BaseController {

    private String PREFIX = "/modular/system/btyWage/";
    private String userName="admin";
    @Autowired
    private BtyWageService btyWageService;
    @Resource
    private BtyWxService btyWxService;
    @Resource
    private DictService dictService;
    /**
     * 跳转到通知列表首页
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:06 PM
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyWage.html";
    }

    /**
     * 合规页面
     *
     * @author zxd
     * @Date 2022/07/20
     */
    @RequestMapping("/hg")
    public String hg() {
        return PREFIX + "btyWageHg.html";
    }

    @RequestMapping("/wage")
    public String wage() {
        return PREFIX + "btyWageList.html";
    }

    @RequestMapping("/hp")
    public String hp() {
        return PREFIX + "hpList.html";
    }
    /**
     * 获取通知列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:06 PM
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String dm,String status, Integer isCompliance) {
        Dict dict1 = dictService.getOne(new QueryWrapper<Dict>().eq("code", "WAGE_START_DATE"));
        Dict dict2 = dictService.getOne(new QueryWrapper<Dict>().eq("code", "WAGE_END_DATE"));

        String startDate = dict1.getName();
        String endDate = dict2.getName();

        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getAccount();
        String username=LoginContextHolder.getContext().getUser().getName();
        //获取是否是督导
        String rem=LoginContextHolder.getContext().getUser().getAvatar();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        //获取当前月的上个月
        String lastMonth= DateUtil.getLastMonth();
        Page<Map<String, Object>> list;
        //noinspection AlibabaUndefineMagicConstant
        String roleName="人事";
        String flag="";
        if(roleNames.contains(roleName)|roleNames.contains("营运总监")|account.equals("15615383333")){
            list = this.btyWageService.list(condition,dm,null,lastMonth,status,null,null,flag,isCompliance,startDate,endDate);
            Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
            return LayuiPageFactory.createPageInfo(wrap);
        }

        if(!(userName.equals(account))){
            flag="8";
            //根据姓名查询是否是督导
                if(!roleNames.contains("督导")){
                list = this.btyWageService.list(condition,dm,null,lastMonth,status,username,null,flag,isCompliance,startDate,endDate);
                if(list.getRecords().size()==0){
                    list = this.btyWageService.list(condition,dm,account,lastMonth,status,null,null,flag,isCompliance,startDate,endDate);
                }
            }else{
                list = this.btyWageService.list(condition,dm,null,lastMonth,status,null,username,flag,isCompliance,startDate,endDate);
                if(list.getRecords().size()==0){
                    list = this.btyWageService.list(condition,dm,account,lastMonth,status,null,username,flag,isCompliance,startDate,endDate);
                }

            }
        }else{

            list = this.btyWageService.list(condition,dm,null,lastMonth,status,null,null,flag,isCompliance,startDate,endDate);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 获取工资导出excel列表
     */
    @RequestMapping(value = "/listExport")
    @ResponseBody
    public Object listExport(String condition,String dm,String status,Integer isCompliance) {
        Dict dict1 = dictService.getOne(new QueryWrapper<Dict>().eq("code", "WAGE_START_DATE"));
        Dict dict2 = dictService.getOne(new QueryWrapper<Dict>().eq("code", "WAGE_END_DATE"));

        String startDate = dict1.getName();
        String endDate = dict2.getName();

        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getAccount();
        String username=LoginContextHolder.getContext().getUser().getName();
        //获取是否是督导
        String rem=LoginContextHolder.getContext().getUser().getAvatar();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        //获取当前月的上个月
        String lastMonth= DateUtil.getLastMonth();
        List<Map<String, Object>> list;
        //noinspection AlibabaUndefineMagicConstant
        String roleName="人事";
        String flag="";
        if(roleNames.contains(roleName)){
            list = this.btyWageService.listExport(condition,dm,null,lastMonth,status,null,null,flag,isCompliance,startDate,endDate);
            Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
            return LayuiPageFactory.createPageInfo(wrap);
        }
        if(!(userName.equals(account))){
            flag="8";
            if(!roleNames.contains("督导")){
           // if(rem.contains(off)){
                list = this.btyWageService.listExport(condition,dm,null,lastMonth,status,username,null,flag,isCompliance,startDate,endDate);
                if(list.size()==0){
                    list = this.btyWageService.listExport(condition,dm,account,lastMonth,status,null,null,flag,isCompliance,startDate,endDate);
                }
            }else{
                list = this.btyWageService.listExport(condition,dm,null,lastMonth,status,null,username,flag,isCompliance,startDate,endDate);
                if(list.size()==0){
                    list = this.btyWageService.listExport(condition,dm,account,lastMonth,status,null,username,flag,isCompliance,startDate,endDate);
                }

            }
        }else{

            list = this.btyWageService.listExport(condition,dm,null,lastMonth,status,null,null,flag,isCompliance,startDate,endDate);
        }
        return list;
    }
    /**
     * 批量审核确认
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmOk")
    @ResponseBody
    public Object confirmOk(@RequestParam String ids) {
        String[] idss = ids.split(",");
        for(String id:idss){
            BtyWage btyWage=btyWageService.getById(id);
            String bz=btyWage.getBz()==null?"":btyWage.getBz();
            String remark=bz+"9";
            btyWage.setBz(remark);
            btyWage.setUpdateTime(new Date());
            btyWage.setUpdateUser(LoginContextHolder.getContext().getUser().getId());
            btyWageService.updateById(btyWage);
        }
        return SUCCESS_TIP;
    }
    /**
     * 批量取消审核
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmQx")
    @ResponseBody
    public Object confirmQx(@RequestParam String ids) {
        String[] idss = ids.split(",");
        for(String id:idss){
            BtyWage btyWage=btyWageService.getById(id);
            String bz=btyWage.getBz()==null?"":btyWage.getBz();
            String remark="";
            if(bz.contains("9")){
                bz=bz.substring(0,bz.indexOf("9"));
                remark=bz;
            }
            btyWage.setBz(remark);
            btyWage.setUpdateTime(new Date());
            btyWage.setUpdateUser(LoginContextHolder.getContext().getUser().getId());
            btyWageService.updateById(btyWage);
        }
        return SUCCESS_TIP;
    }
    /**
     * 查询店名
     *
     * @author liuy
     *
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyArchives> getDictionaryType(Integer isCompliance){
        System.out.println(isCompliance);
        //获取是否是督导
        String rem=LoginContextHolder.getContext().getUser().getAvatar();
        //获取用户的帐号
        String account= LoginContextHolder.getContext().getUser().getName();
        String phone=LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames=LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyArchives> list = null;
        if(roleNames.contains("人事")|roleNames.contains("营运总监")|account.equals("15615383333")){
            account="admin";
            list = this.btyWageService.dmList(account,null,null, isCompliance);
            return list;
        }
        if(!(userName.equals(account))){
            if(!roleNames.contains("督导")){
                 list = this.btyWageService.dmList(account,null,null, isCompliance);
                if(list.size()==0){
                    list = this.btyWageService.dmList(null,null,phone, isCompliance);
                }
            }else{
                list = this.btyWageService.dmList(null,account,null, isCompliance);
                if(list.size()==0){
                    list = this.btyWageService.dmList(null,account,phone, isCompliance);
                }
            }
        }else{
            list = this.btyWageService.dmList(account,null,null, isCompliance);
        }

        return list;
    }

    @RequestMapping(value = "/listWage")
    @ResponseBody
    public Object listWage(String condition,String dm,String status,String bh) {
        //获取当前月的上个月
        String lastMonth= DateUtil.getLastMonth();
        Page<Map<String, Object>> list = this.btyWageService.listWage(condition,dm,lastMonth,bh);
        Page<Map<String, Object>> wrap = new WageWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 获取漏发工资导出excel列表
     */
    @RequestMapping(value = "/listWageExport")
    @ResponseBody
    public Object listWageExport(String condition,String dm,String bh) {
        //获取当前月的上个月
        String lastMonth= DateUtil.getLastMonth();
        List<Map<String, Object>> list=btyWageService.listWageExport(condition,dm,lastMonth,bh);
        return list;
    }

    @RequestMapping(value = "/hpList")
    @ResponseBody
    public Object hpList(String dm,String month) {
        //获取当前月的上个月
        //String lastMonth= DateUtil.getLastMonth();
        Page<Map<String, Object>>list=btyWageService.hpList(month,dm);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 货赔导出excel列表
     * @param dm
     * @param month
     * @return
     */
    @RequestMapping(value = "/listHpExport")
    @ResponseBody
    public Object listHpExport(String dm,String month) {
        List<Map<String, Object>>list=btyWageService.listHpExport(month,dm);
        return list;
    }
}
